package com.atguigu.queue;

import java.util.Scanner;

/**
 * @author durant
 * @create 2021-11-11 19:03
 */
public class CircleArrayQueue {
    public static void main(String[] args) {
        System.out.println("测试环形数组");
        CircleArray aq = new CircleArray(4);// 有一个空间是空出来的，也就是说只有3个有效空间
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show)显示队列");
            System.out.println("a(add)显示队列");
            System.out.println("y(isEmpty)显示队列");
            System.out.println("f(isFull)显示队列");
            System.out.println("e(exit)显示队列");
            System.out.println("g(get)出队列");
            System.out.println("h(head)出队列");
            System.out.println("****************");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    aq.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数：");
                    int value =scanner.nextInt();
                    aq.addQueue(value);

                    break;
                case 'y':
                    System.out.println(aq.isEmpty());

                    break;
                case 'f':
                    System.out.println("队列是否为full："+aq.isFull());
                    break;

                case 'g':
                    try {
                        System.out.println("取出的数据是："+aq.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    System.out.println(aq.headQueue());
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    System.out.println("程序退出了");
                    break;
                default:
                    break;

            }


        }


    }

}
class CircleArray{

    private int maxSize;
    private int front;//
    private int rear;  // 列尾
    private int[] arr;//该数组用于存放数据

    //创建队列的构造器

    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }
    //判断队列是否满
    public boolean isFull() {

        return (rear + 1) % maxSize == front;
    }

    //判断队列是否为空
    public boolean isEmpty() {

        return rear == front;
    }
    //添加数据到队列
    public void addQueue(int n) {
        //判断队列是否满
        if (isFull()) {
            System.out.println("队列满了");
        }
        arr[rear]=n;
        rear = (rear + 1)%maxSize;
    }
    //出队列，从前（front）出数据
    public int getQueue() {
        //判断队列是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
      //这里需要分析出front是指向队列的第一个元素
        // 1.先把front 对应的值保留到一个临时变量
        //2. 将临时的变量返回
        int value = arr[front];
        front = (front + 1)% maxSize;
        return value;
    }
    //显示队列所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，无法遍历");
            return;
        }
       //从front开始遍历，遍历数组长度的元素就可以停止
        for (int i = front; i < front+size(); i++) {
            System.out.println("arr  "+"["+i%maxSize+"]="+arr[i % maxSize]);
        }

    }
    // 求出环形数列的有效数字的个数
    public int size(){

        return (rear + maxSize - front) % maxSize;
    }
    //显示队列的头数据，不是取数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，没有头数据");
        }
        return arr[front];
    }
}
